package br.ufpe.cin.sm.nninn;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import br.ufpe.cin.sm.nninn.maps.Maps;

import com.google.android.maps.MapActivity;

public class NninnAActivity extends MapActivity {
	
	private WebView webView;
	
	private EditText etSearch;
	private Button btSearch;
	private Button btSearchGPS;
	
	private Location location = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//renderizar a pagina html com os hoteis
		webView = new WebView(this);
		
		setContentView(R.layout.main);
		// vai receber como parametro o local para buscar
		
		etSearch = (EditText) findViewById(R.id.editText1);
				
		btSearch = (Button) findViewById(R.id.button1);
		btSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ArrayList<HashMap<String, String>> locations = Maps.getLocations(etSearch.getText().toString());
				if(locations != null) {
					//setupWebView("http://10.0.2.2:8080/nninnS/API/v1/search/"
					setupWebView("http://107.22.195.66:8080/nninnS/API/v1/search/"
						+ locations.get(0).get("latitude") + "/"
						+ locations.get(0).get("longitude"));
				} else {
					Log.d("error", "Locations == null");
				}
			}
		});
		
		btSearchGPS = (Button) findViewById(R.id.button2);
		btSearchGPS.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				getGpsPosition();
				if (location != null) { 
					double latitude = location.getLatitude();
				    double longitude = location.getLongitude();
				    //setupWebView("http://10.0.2.2:8080/nninnS/API/v1/search/"
				    setupWebView("http://107.22.195.66:8080/nninnS/API/v1/search/"
							+ latitude+ "/"+ longitude);
				}
			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void setupWebView(String url){	    
	    webView = (WebView) findViewById(R.id.webview);
	    webView.getSettings().setJavaScriptEnabled(true);
	    
	    //Wait for the page to load then send the location information
	    webView.setWebViewClient(new WebViewClient());
	    Log.i("msg", url);
	    webView.loadUrl(url);
	  }
	
	public void getGpsPosition() {
		
		LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
		location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		if(location != null) {
			//showMyAddress(location);
		}

		LocationListener locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		    	//showMyAddress(location);
		    }

		    public void onProviderDisabled(String arg0) {
		        // TODO Auto-generated method stub
		    }

		    public void onProviderEnabled(String arg0) {
		        // TODO Auto-generated method stub
		    }

		    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		        // TODO Auto-generated method stub
		    }
		};

		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
	}
}
